

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-hexo-fluid/img/fluid.png">
  <link rel="icon" href="https://picgo-wskh.oss-cn-guangzhou.aliyuncs.com/avatar.jpg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="WSKH0929">
  <meta name="keywords" content="">
  
    <meta name="description" content="1 主成分分析可视化结果1.1 查看莺尾花数据集(前五行，前四列)1iris[1:5,-5]  123456##        Sepal.Length Sepal.Width Petal.Length Petal.Width## 1          5.1         3.5          1.4         0.2## 2          4.9         3.0">
<meta property="og:type" content="article">
<meta property="og:title" content="R language visualization">
<meta property="og:url" content="https://wskh0929.gitee.io/2023/01/01/R-language-visualization/index.html">
<meta property="og:site_name" content="WSKH0929 的博客">
<meta property="og:description" content="1 主成分分析可视化结果1.1 查看莺尾花数据集(前五行，前四列)1iris[1:5,-5]  123456##        Sepal.Length Sepal.Width Petal.Length Petal.Width## 1          5.1         3.5          1.4         0.2## 2          4.9         3.0">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://picgo-wskh.oss-cn-guangzhou.aliyuncs.com/20221231155725.png">
<meta property="article:published_time" content="2023-01-01T10:21:18.000Z">
<meta property="article:modified_time" content="2022-12-31T08:00:20.256Z">
<meta property="article:author" content="WSKH0929">
<meta property="article:tag" content="R">
<meta property="article:tag" content="Data Analysis">
<meta property="article:tag" content="Data Visualization">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://picgo-wskh.oss-cn-guangzhou.aliyuncs.com/20221231155725.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>R language visualization - WSKH0929 的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-hexo-fluid/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-hexo-fluid/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-hexo-fluid/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="//at.alicdn.com/t/c/font_3846514_kabxni94auf.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/gundongtiao.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"wskh0929.gitee.io","root":"/my-hexo-fluid/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-hexo-fluid/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-hexo-fluid/js/utils.js" ></script>
  <script  src="/my-hexo-fluid/js/color-schema.js" ></script>
  

  

  

  

  

  

  

  



  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-hexo-fluid/">
      <strong>WSKH0929 的博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/archives">
                <i class="iconfont icon-books"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/categories">
                <i class="iconfont icon-th-large"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-hexo-fluid/links/">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" href="javascript:;" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-book"></i>
                <span>文档</span>
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    <span>主题博客</span>
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_51545953/article/details/128499341?spm=1001.2014.3001.5501">
                    
                    <span>配置指南</span>
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    <span>图标用法</span>
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-hexo-fluid/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="R language visualization"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        WSKH0929
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-01-01 18:21" pubdate>
           2023 年 01 月 01 日 , 晚上 06:21 , 星期日
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          <!-- compatible with older versions-->
          15k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          <!-- compatible with older versions-->
          128 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">R language visualization</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="1-主成分分析可视化结果"><a href="#1-主成分分析可视化结果" class="headerlink" title="1 主成分分析可视化结果"></a>1 主成分分析可视化结果</h1><h2 id="1-1-查看莺尾花数据集-前五行，前四列"><a href="#1-1-查看莺尾花数据集-前五行，前四列" class="headerlink" title="1.1 查看莺尾花数据集(前五行，前四列)"></a>1.1 查看莺尾花数据集(前五行，前四列)</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs R">iris<span class="hljs-punctuation">[</span><span class="hljs-number">1</span><span class="hljs-operator">:</span><span class="hljs-number">5</span><span class="hljs-punctuation">,</span><span class="hljs-operator">-</span><span class="hljs-number">5</span><span class="hljs-punctuation">]</span><br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs R"><span class="hljs-comment">##        Sepal.Length Sepal.Width Petal.Length Petal.Width</span><br><span class="hljs-comment">## 1          5.1         3.5          1.4         0.2</span><br><span class="hljs-comment">## 2          4.9         3.0          1.4         0.2</span><br><span class="hljs-comment">## 3          4.7         3.2          1.3         0.2</span><br><span class="hljs-comment">## 4          4.6         3.1          1.5         0.2</span><br><span class="hljs-comment">## 5          5.0         3.6          1.4         0.2</span><br></code></pre></td></tr></table></figure>

<h2 id="1-2-使用莺尾花数据集进行主成分分析后可视化展示"><a href="#1-2-使用莺尾花数据集进行主成分分析后可视化展示" class="headerlink" title="1.2 使用莺尾花数据集进行主成分分析后可视化展示"></a>1.2 使用莺尾花数据集进行主成分分析后可视化展示</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs r">library<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;ggplot2&quot;</span><span class="hljs-punctuation">)</span><br>library<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;ggbiplot&quot;</span><span class="hljs-punctuation">)</span><br>res.pca <span class="hljs-operator">=</span> prcomp<span class="hljs-punctuation">(</span>iris<span class="hljs-punctuation">[</span><span class="hljs-punctuation">,</span><span class="hljs-operator">-</span><span class="hljs-number">5</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>scale<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">)</span><br>ggbiplot<span class="hljs-punctuation">(</span>res.pca<span class="hljs-punctuation">,</span>obs.scale<span class="hljs-operator">=</span><span class="hljs-number">1</span><span class="hljs-punctuation">,</span>var.scale<span class="hljs-operator">=</span><span class="hljs-number">1</span><span class="hljs-punctuation">,</span>ellipse<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span>circle<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs r"><span class="hljs-comment">#添加组别颜色</span><br>ggbiplot<span class="hljs-punctuation">(</span>res.pca<span class="hljs-punctuation">,</span>obs.scale<span class="hljs-operator">=</span><span class="hljs-number">1</span><span class="hljs-punctuation">,</span>var.scale<span class="hljs-operator">=</span><span class="hljs-number">1</span><span class="hljs-punctuation">,</span>ellipse<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span>circle<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span>groups<span class="hljs-operator">=</span>iris<span class="hljs-operator">$</span>Species<span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs r"><span class="hljs-comment">#更改绘制主题</span><br>ggbiplot<span class="hljs-punctuation">(</span>res.pca<span class="hljs-punctuation">,</span> obs.scale <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span> var.scale <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span> ellipse <span class="hljs-operator">=</span> <span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span>groups <span class="hljs-operator">=</span> iris<span class="hljs-operator">$</span>Species<span class="hljs-punctuation">,</span> circle <span class="hljs-operator">=</span> <span class="hljs-literal">TRUE</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  theme_bw<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  theme<span class="hljs-punctuation">(</span>panel.grid <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  scale_color_brewer<span class="hljs-punctuation">(</span>palette <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Set2&quot;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  labs<span class="hljs-punctuation">(</span>title <span class="hljs-operator">=</span> <span class="hljs-string">&quot;新主题&quot;</span><span class="hljs-punctuation">,</span>subtitle <span class="hljs-operator">=</span> <span class="hljs-string">&quot;好看吗！&quot;</span><span class="hljs-punctuation">,</span>caption <span class="hljs-operator">=</span><span class="hljs-string">&quot;绘于：桂林&quot;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h1 id="2-圆环图绘制"><a href="#2-圆环图绘制" class="headerlink" title="2 圆环图绘制"></a>2 圆环图绘制</h1><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs r"><span class="hljs-comment">#构造数据</span><br>df <span class="hljs-operator">&lt;-</span> data.frame<span class="hljs-punctuation">(</span><br>  group <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Male&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;Female&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;Child&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>  value <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">10</span><span class="hljs-punctuation">,</span> <span class="hljs-number">20</span><span class="hljs-punctuation">,</span> <span class="hljs-number">30</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs r"><span class="hljs-comment">#ggpubr包绘制圆环图</span><br>library<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;ggpubr&quot;</span><span class="hljs-punctuation">)</span><br>ggdonutchart<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;value&quot;</span><span class="hljs-punctuation">,</span><br>             label <span class="hljs-operator">=</span> <span class="hljs-string">&quot;group&quot;</span><span class="hljs-punctuation">,</span>                               <br>             fill <span class="hljs-operator">=</span> <span class="hljs-string">&quot;group&quot;</span><span class="hljs-punctuation">,</span>                            <br>             color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;white&quot;</span><span class="hljs-punctuation">,</span>                                <br>             palette <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-string">&quot;#00AFBB&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;#E7B800&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;#FC4E07&quot;</span><span class="hljs-punctuation">)</span> <br><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h1 id="3-马赛克图绘制"><a href="#3-马赛克图绘制" class="headerlink" title="3 马赛克图绘制"></a>3 马赛克图绘制</h1><h2 id="3-1-构造数据"><a href="#3-1-构造数据" class="headerlink" title="3.1 构造数据"></a>3.1 构造数据</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs r">library<span class="hljs-punctuation">(</span>ggplot2<span class="hljs-punctuation">)</span><br>library<span class="hljs-punctuation">(</span>RColorBrewer<span class="hljs-punctuation">)</span><br>library<span class="hljs-punctuation">(</span>reshape2<span class="hljs-punctuation">)</span>  <span class="hljs-comment">#提供melt()函数</span><br>library<span class="hljs-punctuation">(</span>plyr<span class="hljs-punctuation">)</span>      <span class="hljs-comment">#提供ddply()函数,join()函数</span><br><br>df <span class="hljs-operator">&lt;-</span> data.frame<span class="hljs-punctuation">(</span>segment <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-string">&quot;A&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;B&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;C&quot;</span><span class="hljs-punctuation">,</span><span class="hljs-string">&quot;D&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>                      Alpha <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">2400</span>	<span class="hljs-punctuation">,</span><span class="hljs-number">1200</span><span class="hljs-punctuation">,</span>	<span class="hljs-number">600</span>	<span class="hljs-punctuation">,</span><span class="hljs-number">250</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>                      Beta <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">1000</span>	<span class="hljs-punctuation">,</span><span class="hljs-number">900</span><span class="hljs-punctuation">,</span>	<span class="hljs-number">600</span><span class="hljs-punctuation">,</span>	<span class="hljs-number">250</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>                      Gamma <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">400</span><span class="hljs-punctuation">,</span>	<span class="hljs-number">600</span>	<span class="hljs-punctuation">,</span><span class="hljs-number">400</span><span class="hljs-punctuation">,</span>	<span class="hljs-number">250</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>                      Delta <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">200</span><span class="hljs-punctuation">,</span>	<span class="hljs-number">300</span>	<span class="hljs-punctuation">,</span><span class="hljs-number">400</span><span class="hljs-punctuation">,</span>	<span class="hljs-number">250</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br><br>melt_df<span class="hljs-operator">&lt;-</span>melt<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span>id<span class="hljs-operator">=</span><span class="hljs-string">&quot;segment&quot;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs r"><span class="hljs-comment"># 查看数据</span><br>df<br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs r">   segment Alpha Beta Gamma Delta<br><span class="hljs-number">1</span>       A  <span class="hljs-number">2400</span> <span class="hljs-number">1000</span>   <span class="hljs-number">400</span>   <span class="hljs-number">200</span><br><span class="hljs-number">2</span>       B  <span class="hljs-number">1200</span>  <span class="hljs-number">900</span>   <span class="hljs-number">600</span>   <span class="hljs-number">300</span><br><span class="hljs-number">3</span>       C   <span class="hljs-number">600</span>  <span class="hljs-number">600</span>   <span class="hljs-number">400</span>   <span class="hljs-number">400</span><br><span class="hljs-number">4</span>       D   <span class="hljs-number">250</span>  <span class="hljs-number">250</span>   <span class="hljs-number">250</span>   <span class="hljs-number">250</span><br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs r"><span class="hljs-comment">#计算出每行的最大，最小值，并计算每行各数的百分比。ddply()对data.frame分组计算，并利用join()函数进行两个表格连接。</span><br>segpct<span class="hljs-operator">&lt;-</span>rowSums<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">[</span><span class="hljs-punctuation">,</span><span class="hljs-number">2</span><span class="hljs-operator">:</span>ncol<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">)</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">)</span><br><span class="hljs-keyword">for</span> <span class="hljs-punctuation">(</span>i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span><span class="hljs-operator">:</span>nrow<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">&#123;</span><br>  <span class="hljs-keyword">for</span> <span class="hljs-punctuation">(</span>j <span class="hljs-keyword">in</span> <span class="hljs-number">2</span><span class="hljs-operator">:</span>ncol<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">&#123;</span><br>    df<span class="hljs-punctuation">[</span>i<span class="hljs-punctuation">,</span>j<span class="hljs-punctuation">]</span><span class="hljs-operator">&lt;-</span>df<span class="hljs-punctuation">[</span>i<span class="hljs-punctuation">,</span>j<span class="hljs-punctuation">]</span><span class="hljs-operator">/</span>segpct<span class="hljs-punctuation">[</span>i<span class="hljs-punctuation">]</span><span class="hljs-operator">*</span><span class="hljs-number">100</span>  <span class="hljs-comment">#将数字转换成百分比</span><br>  <span class="hljs-punctuation">&#125;</span><br><span class="hljs-punctuation">&#125;</span><br><br>segpct<span class="hljs-operator">&lt;-</span>segpct<span class="hljs-operator">/</span><span class="hljs-built_in">sum</span><span class="hljs-punctuation">(</span>segpct<span class="hljs-punctuation">)</span><span class="hljs-operator">*</span><span class="hljs-number">100</span><br>df<span class="hljs-operator">$</span>xmax <span class="hljs-operator">&lt;-</span> <span class="hljs-built_in">cumsum</span><span class="hljs-punctuation">(</span>segpct<span class="hljs-punctuation">)</span><br>df<span class="hljs-operator">$</span>xmin <span class="hljs-operator">&lt;-</span> <span class="hljs-punctuation">(</span>df<span class="hljs-operator">$</span>xmax <span class="hljs-operator">-</span> segpct<span class="hljs-punctuation">)</span><br><br>dfm <span class="hljs-operator">&lt;-</span> melt<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span> id <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-string">&quot;segment&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;xmin&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;xmax&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>value.name<span class="hljs-operator">=</span><span class="hljs-string">&quot;percentage&quot;</span><span class="hljs-punctuation">)</span><br>colnames<span class="hljs-punctuation">(</span>dfm<span class="hljs-punctuation">)</span><span class="hljs-punctuation">[</span>ncol<span class="hljs-punctuation">(</span>dfm<span class="hljs-punctuation">)</span><span class="hljs-punctuation">]</span><span class="hljs-operator">&lt;-</span><span class="hljs-string">&quot;percentage&quot;</span><br><br><span class="hljs-comment">#ddply()函数使用自定义统计函数，对data.frame分组计算</span><br>dfm1 <span class="hljs-operator">&lt;-</span> ddply<span class="hljs-punctuation">(</span>dfm<span class="hljs-punctuation">,</span> .<span class="hljs-punctuation">(</span>segment<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> transform<span class="hljs-punctuation">,</span> ymax <span class="hljs-operator">=</span> <span class="hljs-built_in">cumsum</span><span class="hljs-punctuation">(</span>percentage<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>dfm1 <span class="hljs-operator">&lt;-</span> ddply<span class="hljs-punctuation">(</span>dfm1<span class="hljs-punctuation">,</span> .<span class="hljs-punctuation">(</span>segment<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> transform<span class="hljs-punctuation">,</span>ymin <span class="hljs-operator">=</span> ymax <span class="hljs-operator">-</span> percentage<span class="hljs-punctuation">)</span><br>dfm1<span class="hljs-operator">$</span>xtext <span class="hljs-operator">&lt;-</span> with<span class="hljs-punctuation">(</span>dfm1<span class="hljs-punctuation">,</span> xmin <span class="hljs-operator">+</span> <span class="hljs-punctuation">(</span>xmax <span class="hljs-operator">-</span> xmin<span class="hljs-punctuation">)</span><span class="hljs-operator">/</span><span class="hljs-number">2</span><span class="hljs-punctuation">)</span><br>dfm1<span class="hljs-operator">$</span>ytext <span class="hljs-operator">&lt;-</span> with<span class="hljs-punctuation">(</span>dfm1<span class="hljs-punctuation">,</span> ymin <span class="hljs-operator">+</span> <span class="hljs-punctuation">(</span>ymax <span class="hljs-operator">-</span> ymin<span class="hljs-punctuation">)</span><span class="hljs-operator">/</span><span class="hljs-number">2</span><span class="hljs-punctuation">)</span><br><br><span class="hljs-comment">#join()函数，连接两个表格data.frame</span><br>dfm2<span class="hljs-operator">&lt;-</span>join<span class="hljs-punctuation">(</span>melt_df<span class="hljs-punctuation">,</span> dfm1<span class="hljs-punctuation">,</span> by <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-string">&quot;segment&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;variable&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> type <span class="hljs-operator">=</span> <span class="hljs-string">&quot;left&quot;</span><span class="hljs-punctuation">,</span> match <span class="hljs-operator">=</span> <span class="hljs-string">&quot;all&quot;</span><span class="hljs-punctuation">)</span><br><br><span class="hljs-comment"># 查看数据</span><br>dfm2<br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs r"><span class="hljs-comment">##    segment variable value xmin xmax percentage ymax ymin xtext ytext</span><br><span class="hljs-comment">## 1        A    Alpha  2400    0   40         60   60    0    20  30.0</span><br><span class="hljs-comment">## 2        B    Alpha  1200   40   70         40   40    0    55  20.0</span><br><span class="hljs-comment">## 3        C    Alpha   600   70   90         30   30    0    80  15.0</span><br><span class="hljs-comment">## 4        D    Alpha   250   90  100         25   25    0    95  12.5</span><br><span class="hljs-comment">## 5        A     Beta  1000    0   40         25   85   60    20  72.5</span><br><span class="hljs-comment">## 6        B     Beta   900   40   70         30   70   40    55  55.0</span><br><span class="hljs-comment">## 7        C     Beta   600   70   90         30   60   30    80  45.0</span><br><span class="hljs-comment">## 8        D     Beta   250   90  100         25   50   25    95  37.5</span><br><span class="hljs-comment">## 9        A    Gamma   400    0   40         10   95   85    20  90.0</span><br><span class="hljs-comment">## 10       B    Gamma   600   40   70         20   90   70    55  80.0</span><br><span class="hljs-comment">## 11       C    Gamma   400   70   90         20   80   60    80  70.0</span><br><span class="hljs-comment">## 12       D    Gamma   250   90  100         25   75   50    95  62.5</span><br><span class="hljs-comment">## 13       A    Delta   200    0   40          5  100   95    20  97.5</span><br><span class="hljs-comment">## 14       B    Delta   300   40   70         10  100   90    55  95.0</span><br><span class="hljs-comment">## 15       C    Delta   400   70   90         20  100   80    80  90.0</span><br><span class="hljs-comment">## 16       D    Delta   250   90  100         25  100   75    95  87.5</span><br></code></pre></td></tr></table></figure>

<h2 id="3-2-ggplot2包的geom-rect-函数绘制马赛克图"><a href="#3-2-ggplot2包的geom-rect-函数绘制马赛克图" class="headerlink" title="3.2 ggplot2包的geom_rect()函数绘制马赛克图"></a>3.2 ggplot2包的geom_rect()函数绘制马赛克图</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs r">ggplot<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  geom_rect<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>ymin <span class="hljs-operator">=</span> ymin<span class="hljs-punctuation">,</span> ymax <span class="hljs-operator">=</span> ymax<span class="hljs-punctuation">,</span> xmin <span class="hljs-operator">=</span> xmin<span class="hljs-punctuation">,</span> xmax <span class="hljs-operator">=</span> xmax<span class="hljs-punctuation">,</span> fill <span class="hljs-operator">=</span> variable<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>dfm2<span class="hljs-punctuation">,</span>colour <span class="hljs-operator">=</span> <span class="hljs-string">&quot;black&quot;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  geom_text<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>x <span class="hljs-operator">=</span> xtext<span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> ytext<span class="hljs-punctuation">,</span>  label <span class="hljs-operator">=</span> value<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>dfm2 <span class="hljs-punctuation">,</span>size <span class="hljs-operator">=</span> <span class="hljs-number">4</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  geom_text<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>x <span class="hljs-operator">=</span> xtext<span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> <span class="hljs-number">103</span><span class="hljs-punctuation">,</span> label <span class="hljs-operator">=</span> paste<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Seg &quot;</span><span class="hljs-punctuation">,</span> segment<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>dfm2 <span class="hljs-punctuation">,</span>size <span class="hljs-operator">=</span> <span class="hljs-number">4</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  geom_text<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>x <span class="hljs-operator">=</span> <span class="hljs-number">102</span><span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> seq<span class="hljs-punctuation">(</span><span class="hljs-number">12.5</span><span class="hljs-punctuation">,</span><span class="hljs-number">100</span><span class="hljs-punctuation">,</span><span class="hljs-number">25</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> label <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-string">&quot;Alpha&quot;</span><span class="hljs-punctuation">,</span><span class="hljs-string">&quot;Beta&quot;</span><span class="hljs-punctuation">,</span><span class="hljs-string">&quot;Gamma&quot;</span><span class="hljs-punctuation">,</span><span class="hljs-string">&quot;Delta&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> size <span class="hljs-operator">=</span> <span class="hljs-number">4</span><span class="hljs-punctuation">,</span>hjust <span class="hljs-operator">=</span> <span class="hljs-number">0</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  scale_x_continuous<span class="hljs-punctuation">(</span>breaks<span class="hljs-operator">=</span>seq<span class="hljs-punctuation">(</span><span class="hljs-number">0</span><span class="hljs-punctuation">,</span><span class="hljs-number">100</span><span class="hljs-punctuation">,</span><span class="hljs-number">25</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>limits<span class="hljs-operator">=</span><span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">0</span><span class="hljs-punctuation">,</span><span class="hljs-number">110</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  theme<span class="hljs-punctuation">(</span>panel.background<span class="hljs-operator">=</span>element_rect<span class="hljs-punctuation">(</span>fill<span class="hljs-operator">=</span><span class="hljs-string">&quot;white&quot;</span><span class="hljs-punctuation">,</span>colour<span class="hljs-operator">=</span><span class="hljs-literal">NA</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        panel.grid.major <span class="hljs-operator">=</span> element_line<span class="hljs-punctuation">(</span>colour <span class="hljs-operator">=</span> <span class="hljs-string">&quot;grey60&quot;</span><span class="hljs-punctuation">,</span>size<span class="hljs-operator">=</span><span class="hljs-number">.25</span><span class="hljs-punctuation">,</span>linetype <span class="hljs-operator">=</span><span class="hljs-string">&quot;dotted&quot;</span> <span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        panel.grid.minor <span class="hljs-operator">=</span> element_line<span class="hljs-punctuation">(</span>colour <span class="hljs-operator">=</span> <span class="hljs-string">&quot;grey60&quot;</span><span class="hljs-punctuation">,</span>size<span class="hljs-operator">=</span><span class="hljs-number">.25</span><span class="hljs-punctuation">,</span>linetype <span class="hljs-operator">=</span><span class="hljs-string">&quot;dotted&quot;</span> <span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        text<span class="hljs-operator">=</span>element_text<span class="hljs-punctuation">(</span>size<span class="hljs-operator">=</span><span class="hljs-number">15</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        legend.position<span class="hljs-operator">=</span><span class="hljs-string">&quot;none&quot;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h2 id="3-3-vcd包的mosaic-函数绘制马赛克图"><a href="#3-3-vcd包的mosaic-函数绘制马赛克图" class="headerlink" title="3.3 vcd包的mosaic()函数绘制马赛克图"></a>3.3 vcd包的mosaic()函数绘制马赛克图</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs r">library<span class="hljs-punctuation">(</span>vcd<span class="hljs-punctuation">)</span><br>table<span class="hljs-operator">&lt;-</span>xtabs<span class="hljs-punctuation">(</span>value <span class="hljs-operator">~</span>variable<span class="hljs-operator">+</span>segment<span class="hljs-punctuation">,</span> melt_df<span class="hljs-punctuation">)</span><br>mosaic<span class="hljs-punctuation">(</span> <span class="hljs-operator">~</span>segment<span class="hljs-operator">+</span>variable<span class="hljs-punctuation">,</span>table<span class="hljs-punctuation">,</span>shade<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span>legend<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span>color<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h2 id="3-4-graphics包的mosaicplot-函数绘制马赛克图"><a href="#3-4-graphics包的mosaicplot-函数绘制马赛克图" class="headerlink" title="3.4 graphics包的mosaicplot()函数绘制马赛克图"></a>3.4 graphics包的mosaicplot()函数绘制马赛克图</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs R">library<span class="hljs-punctuation">(</span>graphics<span class="hljs-punctuation">)</span><br>library<span class="hljs-punctuation">(</span>wesanderson<span class="hljs-punctuation">)</span> <span class="hljs-comment">#颜色提取</span><br>mosaicplot<span class="hljs-punctuation">(</span> <span class="hljs-operator">~</span>segment<span class="hljs-operator">+</span>variable<span class="hljs-punctuation">,</span>table<span class="hljs-punctuation">,</span> color <span class="hljs-operator">=</span> wes_palette<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;GrandBudapest1&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>main <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;&#x27;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h1 id="4-棒棒糖图绘制"><a href="#4-棒棒糖图绘制" class="headerlink" title="4 棒棒糖图绘制"></a>4 棒棒糖图绘制</h1><h2 id="4-1-查看内置示例数据"><a href="#4-1-查看内置示例数据" class="headerlink" title="4.1 查看内置示例数据"></a>4.1 查看内置示例数据</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs R">library<span class="hljs-punctuation">(</span>ggplot2<span class="hljs-punctuation">)</span><br>data<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;mtcars&quot;</span><span class="hljs-punctuation">)</span><br>df <span class="hljs-operator">&lt;-</span> mtcars<br><span class="hljs-comment"># 转换为因子</span><br>df<span class="hljs-operator">$</span>cyl <span class="hljs-operator">&lt;-</span> as.factor<span class="hljs-punctuation">(</span>df<span class="hljs-operator">$</span>cyl<span class="hljs-punctuation">)</span><br>df<span class="hljs-operator">$</span>name <span class="hljs-operator">&lt;-</span> rownames<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">)</span><br>head<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs R"><span class="hljs-comment">##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb</span><br><span class="hljs-comment">## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4</span><br><span class="hljs-comment">## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4</span><br><span class="hljs-comment">## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1</span><br><span class="hljs-comment">## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1</span><br><span class="hljs-comment">## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2</span><br><span class="hljs-comment">## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1</span><br><span class="hljs-comment">##                                name</span><br><span class="hljs-comment">## Mazda RX4                 Mazda RX4</span><br><span class="hljs-comment">## Mazda RX4 Wag         Mazda RX4 Wag</span><br><span class="hljs-comment">## Datsun 710               Datsun 710</span><br><span class="hljs-comment">## Hornet 4 Drive       Hornet 4 Drive</span><br><span class="hljs-comment">## Hornet Sportabout Hornet Sportabout</span><br><span class="hljs-comment">## Valiant                     Valiant</span><br></code></pre></td></tr></table></figure>

<h2 id="4-2-绘制基础棒棒糖图（使用ggplot2）"><a href="#4-2-绘制基础棒棒糖图（使用ggplot2）" class="headerlink" title="4.2 绘制基础棒棒糖图（使用ggplot2）"></a>4.2 绘制基础棒棒糖图（使用ggplot2）</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs R">ggplot<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span>aes<span class="hljs-punctuation">(</span>name<span class="hljs-punctuation">,</span>mpg<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  <span class="hljs-comment"># 添加散点</span><br>  geom_point<span class="hljs-punctuation">(</span>size<span class="hljs-operator">=</span><span class="hljs-number">5</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  <span class="hljs-comment"># 添加辅助线段</span><br>  geom_segment<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>x<span class="hljs-operator">=</span>name<span class="hljs-punctuation">,</span>xend<span class="hljs-operator">=</span>name<span class="hljs-punctuation">,</span>y<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-punctuation">,</span>yend<span class="hljs-operator">=</span>mpg<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h3 id="4-2-1-更改点的大小，形状，颜色和透明度"><a href="#4-2-1-更改点的大小，形状，颜色和透明度" class="headerlink" title="4.2.1 更改点的大小，形状，颜色和透明度"></a>4.2.1 更改点的大小，形状，颜色和透明度</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs R">ggplot<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span>aes<span class="hljs-punctuation">(</span>name<span class="hljs-punctuation">,</span>mpg<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  <span class="hljs-comment"># 添加散点</span><br>  geom_point<span class="hljs-punctuation">(</span>size<span class="hljs-operator">=</span><span class="hljs-number">5</span><span class="hljs-punctuation">,</span> color<span class="hljs-operator">=</span><span class="hljs-string">&quot;red&quot;</span><span class="hljs-punctuation">,</span> fill<span class="hljs-operator">=</span>alpha<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;orange&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-number">0.3</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <br>             alpha<span class="hljs-operator">=</span><span class="hljs-number">0.7</span><span class="hljs-punctuation">,</span> shape<span class="hljs-operator">=</span><span class="hljs-number">21</span><span class="hljs-punctuation">,</span> stroke<span class="hljs-operator">=</span><span class="hljs-number">3</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  <span class="hljs-comment"># 添加辅助线段</span><br>  geom_segment<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>x<span class="hljs-operator">=</span>name<span class="hljs-punctuation">,</span>xend<span class="hljs-operator">=</span>name<span class="hljs-punctuation">,</span>y<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-punctuation">,</span>yend<span class="hljs-operator">=</span>mpg<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  theme_bw<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  theme<span class="hljs-punctuation">(</span>axis.text.x <span class="hljs-operator">=</span> element_text<span class="hljs-punctuation">(</span>angle <span class="hljs-operator">=</span> <span class="hljs-number">45</span><span class="hljs-punctuation">,</span>hjust <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        panel.grid <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h3 id="4-2-2-更改辅助线段的大小，颜色和类型"><a href="#4-2-2-更改辅助线段的大小，颜色和类型" class="headerlink" title="4.2.2 更改辅助线段的大小，颜色和类型"></a>4.2.2 更改辅助线段的大小，颜色和类型</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs R">ggplot<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span>aes<span class="hljs-punctuation">(</span>name<span class="hljs-punctuation">,</span>mpg<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  <span class="hljs-comment"># 添加散点</span><br>  geom_point<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>size<span class="hljs-operator">=</span>cyl<span class="hljs-punctuation">,</span>color<span class="hljs-operator">=</span>cyl<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  <span class="hljs-comment"># 添加辅助线段</span><br>  geom_segment<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>x<span class="hljs-operator">=</span>name<span class="hljs-punctuation">,</span>xend<span class="hljs-operator">=</span>name<span class="hljs-punctuation">,</span>y<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-punctuation">,</span>yend<span class="hljs-operator">=</span>mpg<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>               size<span class="hljs-operator">=</span><span class="hljs-number">1</span><span class="hljs-punctuation">,</span> color<span class="hljs-operator">=</span><span class="hljs-string">&quot;blue&quot;</span><span class="hljs-punctuation">,</span> linetype<span class="hljs-operator">=</span><span class="hljs-string">&quot;dotdash&quot;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  theme_classic<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  theme<span class="hljs-punctuation">(</span>axis.text.x <span class="hljs-operator">=</span> element_text<span class="hljs-punctuation">(</span>angle <span class="hljs-operator">=</span> <span class="hljs-number">45</span><span class="hljs-punctuation">,</span>hjust <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        panel.grid <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  scale_y_continuous<span class="hljs-punctuation">(</span>expand <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">0</span><span class="hljs-punctuation">,</span><span class="hljs-number">0</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h3 id="4-2-3-对点进行排序，坐标轴翻转"><a href="#4-2-3-对点进行排序，坐标轴翻转" class="headerlink" title="4.2.3 对点进行排序，坐标轴翻转"></a>4.2.3 对点进行排序，坐标轴翻转</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs R">df <span class="hljs-operator">&lt;-</span> df<span class="hljs-punctuation">[</span>order<span class="hljs-punctuation">(</span>df<span class="hljs-operator">$</span>mpg<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><span class="hljs-punctuation">]</span><br><span class="hljs-comment"># 设置因子进行排序</span><br>df<span class="hljs-operator">$</span>name <span class="hljs-operator">&lt;-</span> factor<span class="hljs-punctuation">(</span>df<span class="hljs-operator">$</span>name<span class="hljs-punctuation">,</span>levels <span class="hljs-operator">=</span> df<span class="hljs-operator">$</span>name<span class="hljs-punctuation">)</span><br><br>ggplot<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span>aes<span class="hljs-punctuation">(</span>name<span class="hljs-punctuation">,</span>mpg<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  <span class="hljs-comment"># 添加散点</span><br>  geom_point<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>color<span class="hljs-operator">=</span>cyl<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>size<span class="hljs-operator">=</span><span class="hljs-number">8</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  <span class="hljs-comment"># 添加辅助线段</span><br>  geom_segment<span class="hljs-punctuation">(</span>aes<span class="hljs-punctuation">(</span>x<span class="hljs-operator">=</span>name<span class="hljs-punctuation">,</span>xend<span class="hljs-operator">=</span>name<span class="hljs-punctuation">,</span>y<span class="hljs-operator">=</span><span class="hljs-number">0</span><span class="hljs-punctuation">,</span>yend<span class="hljs-operator">=</span>mpg<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>               size<span class="hljs-operator">=</span><span class="hljs-number">1</span><span class="hljs-punctuation">,</span> color<span class="hljs-operator">=</span><span class="hljs-string">&quot;gray&quot;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  theme_minimal<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <br>  theme<span class="hljs-punctuation">(</span><br>    panel.grid.major.y <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>    panel.border <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>    axis.ticks.y <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><br>  <span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  coord_flip<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h2 id="4-3-绘制棒棒糖图（使用ggpubr）"><a href="#4-3-绘制棒棒糖图（使用ggpubr）" class="headerlink" title="4.3 绘制棒棒糖图（使用ggpubr）"></a>4.3 绘制棒棒糖图（使用ggpubr）</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs R">library<span class="hljs-punctuation">(</span>ggpubr<span class="hljs-punctuation">)</span><br><span class="hljs-comment"># 查看示例数据</span><br>head<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs R"><span class="hljs-comment">##                      mpg cyl disp  hp drat    wt  qsec vs am gear carb</span><br><span class="hljs-comment">## Cadillac Fleetwood  10.4   8  472 205 2.93 5.250 17.98  0  0    3    4</span><br><span class="hljs-comment">## Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4</span><br><span class="hljs-comment">## Camaro Z28          13.3   8  350 245 3.73 3.840 15.41  0  0    3    4</span><br><span class="hljs-comment">## Duster 360          14.3   8  360 245 3.21 3.570 15.84  0  0    3    4</span><br><span class="hljs-comment">## Chrysler Imperial   14.7   8  440 230 3.23 5.345 17.42  0  0    3    4</span><br><span class="hljs-comment">## Maserati Bora       15.0   8  301 335 3.54 3.570 14.60  0  1    5    8</span><br><span class="hljs-comment">##                                    name</span><br><span class="hljs-comment">## Cadillac Fleetwood   Cadillac Fleetwood</span><br><span class="hljs-comment">## Lincoln Continental Lincoln Continental</span><br><span class="hljs-comment">## Camaro Z28                   Camaro Z28</span><br><span class="hljs-comment">## Duster 360                   Duster 360</span><br><span class="hljs-comment">## Chrysler Imperial     Chrysler Imperial</span><br><span class="hljs-comment">## Maserati Bora             Maserati Bora</span><br></code></pre></td></tr></table></figure>

<h3 id="4-3-1-使用ggdotchart函数绘制棒棒糖图"><a href="#4-3-1-使用ggdotchart函数绘制棒棒糖图" class="headerlink" title="4.3.1 使用ggdotchart函数绘制棒棒糖图"></a>4.3.1 使用ggdotchart函数绘制棒棒糖图</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs R">ggdotchart<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span> x <span class="hljs-operator">=</span> <span class="hljs-string">&quot;name&quot;</span><span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> <span class="hljs-string">&quot;mpg&quot;</span><span class="hljs-punctuation">,</span><br>           color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;cyl&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置按照cyl填充颜色</span><br>           size <span class="hljs-operator">=</span> <span class="hljs-number">6</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置点的大小</span><br>           palette <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-string">&quot;#00AFBB&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;#E7B800&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;#FC4E07&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 修改颜色画板</span><br>           sorting <span class="hljs-operator">=</span> <span class="hljs-string">&quot;ascending&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置升序排序                        </span><br>           add <span class="hljs-operator">=</span> <span class="hljs-string">&quot;segments&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 添加辅助线段</span><br>           add.params <span class="hljs-operator">=</span> <span class="hljs-built_in">list</span><span class="hljs-punctuation">(</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;lightgray&quot;</span><span class="hljs-punctuation">,</span> size <span class="hljs-operator">=</span> <span class="hljs-number">1.5</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置辅助线段的大小和颜色</span><br>           ggtheme <span class="hljs-operator">=</span> theme_pubr<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置主题</span><br><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h3 id="4-3-2-自定义一些参数"><a href="#4-3-2-自定义一些参数" class="headerlink" title="4.3.2 自定义一些参数"></a>4.3.2 自定义一些参数</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs R">ggdotchart<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span> x <span class="hljs-operator">=</span> <span class="hljs-string">&quot;name&quot;</span><span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> <span class="hljs-string">&quot;mpg&quot;</span><span class="hljs-punctuation">,</span><br>           color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;cyl&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置按照cyl填充颜色</span><br>           size <span class="hljs-operator">=</span> <span class="hljs-number">8</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置点的大小</span><br>           palette <span class="hljs-operator">=</span> <span class="hljs-string">&quot;jco&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 修改颜色画板</span><br>           sorting <span class="hljs-operator">=</span> <span class="hljs-string">&quot;descending&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置降序排序                        </span><br>           add <span class="hljs-operator">=</span> <span class="hljs-string">&quot;segments&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 添加辅助线段</span><br>           add.params <span class="hljs-operator">=</span> <span class="hljs-built_in">list</span><span class="hljs-punctuation">(</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;lightgray&quot;</span><span class="hljs-punctuation">,</span> size <span class="hljs-operator">=</span> <span class="hljs-number">1.2</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置辅助线段的大小和颜色</span><br>           rotate <span class="hljs-operator">=</span> <span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 旋转坐标轴方向</span><br>           group <span class="hljs-operator">=</span> <span class="hljs-string">&quot;cyl&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置按照cyl进行分组</span><br>           label <span class="hljs-operator">=</span> <span class="hljs-string">&quot;mpg&quot;</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 按mpg添加label标签</span><br>           font.label <span class="hljs-operator">=</span> <span class="hljs-built_in">list</span><span class="hljs-punctuation">(</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;white&quot;</span><span class="hljs-punctuation">,</span> <br>                             size <span class="hljs-operator">=</span> <span class="hljs-number">7</span><span class="hljs-punctuation">,</span> <br>                             vjust <span class="hljs-operator">=</span> <span class="hljs-number">0.5</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置label标签的字体颜色和大小</span><br>           ggtheme <span class="hljs-operator">=</span> theme_pubclean<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <span class="hljs-comment"># 设置主题</span><br><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="C:\Users\王孙康宏\AppData\Roaming\Typora\typora-user-images\image-20210830202240043.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="image-20210830202240043"></p>
<h1 id="5-三相元图绘制"><a href="#5-三相元图绘制" class="headerlink" title="5 三相元图绘制"></a>5 三相元图绘制</h1><h2 id="5-1-构建数据"><a href="#5-1-构建数据" class="headerlink" title="5.1 构建数据"></a>5.1 构建数据</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs R">test_data <span class="hljs-operator">=</span> data.frame<span class="hljs-punctuation">(</span>x <span class="hljs-operator">=</span> runif<span class="hljs-punctuation">(</span><span class="hljs-number">100</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>                       y <span class="hljs-operator">=</span> runif<span class="hljs-punctuation">(</span><span class="hljs-number">100</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>                       z <span class="hljs-operator">=</span> runif<span class="hljs-punctuation">(</span><span class="hljs-number">100</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>head<span class="hljs-punctuation">(</span>test_data<span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs R"><span class="hljs-comment">##            x         y         z</span><br><span class="hljs-comment">## 1 0.76397014 0.9721200 0.2028370</span><br><span class="hljs-comment">## 2 0.18523967 0.3208894 0.5175897</span><br><span class="hljs-comment">## 3 0.77091977 0.8440992 0.2677213</span><br><span class="hljs-comment">## 4 0.05159963 0.5317710 0.4060822</span><br><span class="hljs-comment">## 5 0.36533773 0.8701535 0.3750520</span><br><span class="hljs-comment">## 6 0.65153516 0.9035207 0.7910373</span><br></code></pre></td></tr></table></figure>

<h3 id="5-1-1-R-ggtern包绘制三相元图"><a href="#5-1-1-R-ggtern包绘制三相元图" class="headerlink" title="5.1.1 R-ggtern包绘制三相元图"></a>5.1.1 R-ggtern包绘制三相元图</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs R">library<span class="hljs-punctuation">(</span>tidyverse<span class="hljs-punctuation">)</span><br>library<span class="hljs-punctuation">(</span>ggtern<span class="hljs-punctuation">)</span><br>library<span class="hljs-punctuation">(</span>hrbrthemes<span class="hljs-punctuation">)</span><br>library<span class="hljs-punctuation">(</span>ggtext<span class="hljs-punctuation">)</span><br><br>test_plot_pir <span class="hljs-operator">&lt;-</span> ggtern<span class="hljs-punctuation">(</span>data <span class="hljs-operator">=</span> test_data<span class="hljs-punctuation">,</span>aes<span class="hljs-punctuation">(</span>x<span class="hljs-punctuation">,</span> y<span class="hljs-punctuation">,</span> z<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>    geom_point<span class="hljs-punctuation">(</span>size<span class="hljs-operator">=</span><span class="hljs-number">2.5</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>    theme_rgbw<span class="hljs-punctuation">(</span>base_family <span class="hljs-operator">=</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>    labs<span class="hljs-punctuation">(</span>x<span class="hljs-operator">=</span><span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span>y<span class="hljs-operator">=</span><span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>        title <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Example Density/Contour Plot: &lt;span style=&#x27;color:#D20F26&#x27;&gt;GGtern Test&lt;/span&gt;&quot;</span><span class="hljs-punctuation">,</span><br>        subtitle <span class="hljs-operator">=</span> <span class="hljs-string">&quot;processed map charts with &lt;span style=&#x27;color:#1A73E8&#x27;&gt;ggtern()&lt;/span&gt;&quot;</span><span class="hljs-punctuation">,</span><br>        caption <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Visualization by &lt;span style=&#x27;color:#DD6449&#x27;&gt;DataCharm&lt;/span&gt;&quot;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>    guides<span class="hljs-punctuation">(</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;none&quot;</span><span class="hljs-punctuation">,</span> fill <span class="hljs-operator">=</span> <span class="hljs-string">&quot;none&quot;</span><span class="hljs-punctuation">,</span> alpha <span class="hljs-operator">=</span> <span class="hljs-string">&quot;none&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>    theme<span class="hljs-punctuation">(</span><br>        plot.title <span class="hljs-operator">=</span> element_markdown<span class="hljs-punctuation">(</span>hjust <span class="hljs-operator">=</span> <span class="hljs-number">0.5</span><span class="hljs-punctuation">,</span>vjust <span class="hljs-operator">=</span> <span class="hljs-number">.5</span><span class="hljs-punctuation">,</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;black&quot;</span><span class="hljs-punctuation">,</span><br>                             size <span class="hljs-operator">=</span> <span class="hljs-number">20</span><span class="hljs-punctuation">,</span> margin <span class="hljs-operator">=</span> margin<span class="hljs-punctuation">(</span>t <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span> b <span class="hljs-operator">=</span> <span class="hljs-number">12</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        plot.subtitle <span class="hljs-operator">=</span> element_markdown<span class="hljs-punctuation">(</span>hjust <span class="hljs-operator">=</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>vjust <span class="hljs-operator">=</span> <span class="hljs-number">.5</span><span class="hljs-punctuation">,</span>size<span class="hljs-operator">=</span><span class="hljs-number">15</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        plot.caption <span class="hljs-operator">=</span> element_markdown<span class="hljs-punctuation">(</span>face <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;bold&#x27;</span><span class="hljs-punctuation">,</span>size <span class="hljs-operator">=</span> <span class="hljs-number">12</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-punctuation">)</span><br>test_plot_pir<br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h3 id="5-1-2-优化处理"><a href="#5-1-2-优化处理" class="headerlink" title="5.1.2 优化处理"></a>5.1.2 优化处理</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs R">test_plot <span class="hljs-operator">&lt;-</span> ggtern<span class="hljs-punctuation">(</span>data <span class="hljs-operator">=</span> test_data<span class="hljs-punctuation">,</span>aes<span class="hljs-punctuation">(</span>x<span class="hljs-punctuation">,</span> y<span class="hljs-punctuation">,</span> z<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>size<span class="hljs-operator">=</span><span class="hljs-number">2</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>    stat_density_tern<span class="hljs-punctuation">(</span>geom <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;polygon&#x27;</span><span class="hljs-punctuation">,</span>n <span class="hljs-operator">=</span> <span class="hljs-number">300</span><span class="hljs-punctuation">,</span><br>                      aes<span class="hljs-punctuation">(</span>fill  <span class="hljs-operator">=</span> ..level..<span class="hljs-punctuation">,</span><br>                          alpha <span class="hljs-operator">=</span> ..level..<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>    geom_point<span class="hljs-punctuation">(</span>size<span class="hljs-operator">=</span><span class="hljs-number">2.5</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>    theme_rgbw<span class="hljs-punctuation">(</span>base_family <span class="hljs-operator">=</span> <span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>    labs<span class="hljs-punctuation">(</span>x<span class="hljs-operator">=</span><span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span>y<span class="hljs-operator">=</span><span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span><br>        title <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Example Density/Contour Plot: &lt;span style=&#x27;color:#D20F26&#x27;&gt;GGtern Test&lt;/span&gt;&quot;</span><span class="hljs-punctuation">,</span><br>        subtitle <span class="hljs-operator">=</span> <span class="hljs-string">&quot;processed map charts with &lt;span style=&#x27;color:#1A73E8&#x27;&gt;ggtern()&lt;/span&gt;&quot;</span><span class="hljs-punctuation">,</span><br>        caption <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Visualization by &lt;span style=&#x27;color:#DD6449&#x27;&gt;DataCharm&lt;/span&gt;&quot;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>    scale_fill_gradient<span class="hljs-punctuation">(</span>low <span class="hljs-operator">=</span> <span class="hljs-string">&quot;blue&quot;</span><span class="hljs-punctuation">,</span>high <span class="hljs-operator">=</span> <span class="hljs-string">&quot;red&quot;</span><span class="hljs-punctuation">)</span>  <span class="hljs-operator">+</span><br>    <span class="hljs-comment">#去除映射属性的图例</span><br>    guides<span class="hljs-punctuation">(</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;none&quot;</span><span class="hljs-punctuation">,</span> fill <span class="hljs-operator">=</span> <span class="hljs-string">&quot;none&quot;</span><span class="hljs-punctuation">,</span> alpha <span class="hljs-operator">=</span> <span class="hljs-string">&quot;none&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span> <br>    theme<span class="hljs-punctuation">(</span><br>        plot.title <span class="hljs-operator">=</span> element_markdown<span class="hljs-punctuation">(</span>hjust <span class="hljs-operator">=</span> <span class="hljs-number">0.5</span><span class="hljs-punctuation">,</span>vjust <span class="hljs-operator">=</span> <span class="hljs-number">.5</span><span class="hljs-punctuation">,</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;black&quot;</span><span class="hljs-punctuation">,</span><br>                             size <span class="hljs-operator">=</span> <span class="hljs-number">20</span><span class="hljs-punctuation">,</span> margin <span class="hljs-operator">=</span> margin<span class="hljs-punctuation">(</span>t <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-punctuation">,</span> b <span class="hljs-operator">=</span> <span class="hljs-number">12</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        plot.subtitle <span class="hljs-operator">=</span> element_markdown<span class="hljs-punctuation">(</span>hjust <span class="hljs-operator">=</span> <span class="hljs-number">0</span><span class="hljs-punctuation">,</span>vjust <span class="hljs-operator">=</span> <span class="hljs-number">.5</span><span class="hljs-punctuation">,</span>size<span class="hljs-operator">=</span><span class="hljs-number">15</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        plot.caption <span class="hljs-operator">=</span> element_markdown<span class="hljs-punctuation">(</span>face <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;bold&#x27;</span><span class="hljs-punctuation">,</span>size <span class="hljs-operator">=</span> <span class="hljs-number">12</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-punctuation">)</span><br>test_plot<br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h1 id="6-华夫饼图绘制"><a href="#6-华夫饼图绘制" class="headerlink" title="6 华夫饼图绘制"></a>6 华夫饼图绘制</h1><h2 id="6-1-数据准备"><a href="#6-1-数据准备" class="headerlink" title="6.1 数据准备"></a>6.1 数据准备</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs R"><span class="hljs-comment">#相关包</span><br>library<span class="hljs-punctuation">(</span>ggplot2<span class="hljs-punctuation">)</span><br>library<span class="hljs-punctuation">(</span>RColorBrewer<span class="hljs-punctuation">)</span><br>library<span class="hljs-punctuation">(</span>reshape2<span class="hljs-punctuation">)</span><br><span class="hljs-comment">#数据生成</span><br>nrows <span class="hljs-operator">&lt;-</span> 10<br>categ_table <span class="hljs-operator">&lt;-</span> <span class="hljs-built_in">round</span><span class="hljs-punctuation">(</span>table<span class="hljs-punctuation">(</span>mpg<span class="hljs-operator">$</span><span class="hljs-built_in">class</span> <span class="hljs-punctuation">)</span> <span class="hljs-operator">*</span> <span class="hljs-punctuation">(</span><span class="hljs-punctuation">(</span>nrows<span class="hljs-operator">*</span>nrows<span class="hljs-punctuation">)</span><span class="hljs-operator">/</span><span class="hljs-punctuation">(</span><span class="hljs-built_in">length</span><span class="hljs-punctuation">(</span>mpg<span class="hljs-operator">$</span><span class="hljs-built_in">class</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>sort_table<span class="hljs-operator">&lt;-</span>sort<span class="hljs-punctuation">(</span>categ_table<span class="hljs-punctuation">,</span>index.return<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span>decreasing <span class="hljs-operator">=</span> <span class="hljs-literal">FALSE</span><span class="hljs-punctuation">)</span><br>Order<span class="hljs-operator">&lt;-</span>sort<span class="hljs-punctuation">(</span>as.data.frame<span class="hljs-punctuation">(</span>categ_table<span class="hljs-punctuation">)</span><span class="hljs-operator">$</span>Freq<span class="hljs-punctuation">,</span>index.return<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span>decreasing <span class="hljs-operator">=</span> <span class="hljs-literal">FALSE</span><span class="hljs-punctuation">)</span><br>df <span class="hljs-operator">&lt;-</span> expand.grid<span class="hljs-punctuation">(</span>y <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-operator">:</span>nrows<span class="hljs-punctuation">,</span> x <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-operator">:</span>nrows<span class="hljs-punctuation">)</span><br>df<span class="hljs-operator">$</span>category<span class="hljs-operator">&lt;-</span>factor<span class="hljs-punctuation">(</span><span class="hljs-built_in">rep</span><span class="hljs-punctuation">(</span><span class="hljs-built_in">names</span><span class="hljs-punctuation">(</span>sort_table<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>sort_table<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> levels<span class="hljs-operator">=</span><span class="hljs-built_in">names</span><span class="hljs-punctuation">(</span>sort_table<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>Color<span class="hljs-operator">&lt;-</span>brewer.pal<span class="hljs-punctuation">(</span><span class="hljs-built_in">length</span><span class="hljs-punctuation">(</span>sort_table<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;Set2&quot;</span><span class="hljs-punctuation">)</span><br>head<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs R"><span class="hljs-comment">##   y x category</span><br><span class="hljs-comment">## 1 1 1  2seater</span><br><span class="hljs-comment">## 2 2 1  2seater</span><br><span class="hljs-comment">## 3 3 1  minivan</span><br><span class="hljs-comment">## 4 4 1  minivan</span><br><span class="hljs-comment">## 5 5 1  minivan</span><br><span class="hljs-comment">## 6 6 1  minivan</span><br></code></pre></td></tr></table></figure>

<h3 id="6-1-1-ggplot-包绘制"><a href="#6-1-1-ggplot-包绘制" class="headerlink" title="6.1.1 ggplot 包绘制"></a>6.1.1 ggplot 包绘制</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs R">ggplot<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span> aes<span class="hljs-punctuation">(</span>x <span class="hljs-operator">=</span> y<span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> x<span class="hljs-punctuation">,</span> fill <span class="hljs-operator">=</span> category<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>geom_tile<span class="hljs-punctuation">(</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;white&quot;</span><span class="hljs-punctuation">,</span> size <span class="hljs-operator">=</span> <span class="hljs-number">0.25</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br><span class="hljs-comment">#geom_point(color = &quot;black&quot;,shape=1,size=5) +</span><br>coord_fixed<span class="hljs-punctuation">(</span>ratio <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span> <span class="hljs-comment">#x,y 轴尺寸固定， ratio=1 表示 x ， y 轴长度相同</span><br>scale_x_continuous<span class="hljs-punctuation">(</span>trans <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;reverse&#x27;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><span class="hljs-comment">#expand = c(0, 0),</span><br>scale_y_continuous<span class="hljs-punctuation">(</span>trans <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;reverse&#x27;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><span class="hljs-comment">#expand = c(0, 0),</span><br>scale_fill_manual<span class="hljs-punctuation">(</span>name <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Category&quot;</span><span class="hljs-punctuation">,</span><br><span class="hljs-comment">#labels = names(sort_table),</span><br>values <span class="hljs-operator">=</span> Color<span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>theme<span class="hljs-punctuation">(</span><span class="hljs-comment">#panel.border = element_rect(fill=NA,size = 2),</span><br>panel.background <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>plot.title <span class="hljs-operator">=</span> element_text<span class="hljs-punctuation">(</span>size <span class="hljs-operator">=</span> rel<span class="hljs-punctuation">(</span><span class="hljs-number">1.2</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>axis.text <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>axis.title <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>axis.ticks <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>legend.title <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>legend.position <span class="hljs-operator">=</span> <span class="hljs-string">&quot;right&quot;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h3 id="6-1-2-点状华夫饼图ggplot绘制"><a href="#6-1-2-点状华夫饼图ggplot绘制" class="headerlink" title="6.1.2 点状华夫饼图ggplot绘制"></a>6.1.2 点状华夫饼图ggplot绘制</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs R">library<span class="hljs-punctuation">(</span>ggforce<span class="hljs-punctuation">)</span><br>ggplot<span class="hljs-punctuation">(</span>df<span class="hljs-punctuation">,</span> aes<span class="hljs-punctuation">(</span>x0 <span class="hljs-operator">=</span> y<span class="hljs-punctuation">,</span> y0 <span class="hljs-operator">=</span> x<span class="hljs-punctuation">,</span> fill <span class="hljs-operator">=</span> category<span class="hljs-punctuation">,</span>r<span class="hljs-operator">=</span><span class="hljs-number">0.5</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  geom_circle<span class="hljs-punctuation">(</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;black&quot;</span><span class="hljs-punctuation">,</span> size <span class="hljs-operator">=</span> <span class="hljs-number">0.25</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  <span class="hljs-comment">#geom_point(color = &quot;black&quot;,shape=21,size=6) +</span><br>  coord_fixed<span class="hljs-punctuation">(</span>ratio <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  scale_x_continuous<span class="hljs-punctuation">(</span>trans <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;reverse&#x27;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><span class="hljs-comment">#expand = c(0, 0),</span><br>  scale_y_continuous<span class="hljs-punctuation">(</span>trans <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;reverse&#x27;</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><span class="hljs-comment">#expand = c(0, 0),</span><br>  scale_fill_manual<span class="hljs-punctuation">(</span>name <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Category&quot;</span><span class="hljs-punctuation">,</span><br>                    <span class="hljs-comment">#labels = names(sort_table),</span><br>                    values <span class="hljs-operator">=</span> Color<span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  theme<span class="hljs-punctuation">(</span><span class="hljs-comment">#panel.border = element_rect(fill=NA,size = 2),</span><br>    panel.background  <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>    plot.title <span class="hljs-operator">=</span> element_text<span class="hljs-punctuation">(</span>size <span class="hljs-operator">=</span> rel<span class="hljs-punctuation">(</span><span class="hljs-number">1.2</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>    legend.position <span class="hljs-operator">=</span> <span class="hljs-string">&quot;right&quot;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h3 id="6-1-3-堆积型华夫饼图"><a href="#6-1-3-堆积型华夫饼图" class="headerlink" title="6.1.3 堆积型华夫饼图"></a>6.1.3 堆积型华夫饼图</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs R">library<span class="hljs-punctuation">(</span>dplyr<span class="hljs-punctuation">)</span><br>nrows <span class="hljs-operator">&lt;-</span> 10<br>ndeep <span class="hljs-operator">&lt;-</span> 10<br>unit<span class="hljs-operator">&lt;-</span><span class="hljs-number">100</span><br>df <span class="hljs-operator">&lt;-</span> expand.grid<span class="hljs-punctuation">(</span>y <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-operator">:</span>nrows<span class="hljs-punctuation">,</span> x <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-operator">:</span>nrows<span class="hljs-punctuation">)</span><br><br>categ_table <span class="hljs-operator">&lt;-</span> as.data.frame<span class="hljs-punctuation">(</span>table<span class="hljs-punctuation">(</span>mpg<span class="hljs-operator">$</span><span class="hljs-built_in">class</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">*</span> <span class="hljs-punctuation">(</span>nrows<span class="hljs-operator">*</span>nrows<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>colnames<span class="hljs-punctuation">(</span>categ_table<span class="hljs-punctuation">)</span><span class="hljs-operator">&lt;-</span><span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-string">&quot;names&quot;</span><span class="hljs-punctuation">,</span><span class="hljs-string">&quot;vals&quot;</span><span class="hljs-punctuation">)</span><br>categ_table<span class="hljs-operator">&lt;-</span>arrange<span class="hljs-punctuation">(</span>categ_table<span class="hljs-punctuation">,</span>desc<span class="hljs-punctuation">(</span>vals<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>categ_table<span class="hljs-operator">$</span>vals<span class="hljs-operator">&lt;-</span>categ_table<span class="hljs-operator">$</span>vals <span class="hljs-operator">/</span>unit<br><br><br>tb4waffles <span class="hljs-operator">&lt;-</span> expand.grid<span class="hljs-punctuation">(</span>y <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-operator">:</span>ndeep<span class="hljs-punctuation">,</span>x <span class="hljs-operator">=</span> <span class="hljs-built_in">seq_len</span><span class="hljs-punctuation">(</span><span class="hljs-built_in">ceiling</span><span class="hljs-punctuation">(</span><span class="hljs-built_in">sum</span><span class="hljs-punctuation">(</span>categ_table<span class="hljs-operator">$</span>vals<span class="hljs-punctuation">)</span> <span class="hljs-operator">/</span> ndeep<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>regionvec <span class="hljs-operator">&lt;-</span> <span class="hljs-built_in">as.character</span><span class="hljs-punctuation">(</span><span class="hljs-built_in">rep</span><span class="hljs-punctuation">(</span>categ_table<span class="hljs-operator">$</span><span class="hljs-built_in">names</span><span class="hljs-punctuation">,</span> categ_table<span class="hljs-operator">$</span>vals<span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>tb4waffles<span class="hljs-operator">&lt;-</span>tb4waffles<span class="hljs-punctuation">[</span><span class="hljs-number">1</span><span class="hljs-operator">:</span><span class="hljs-built_in">length</span><span class="hljs-punctuation">(</span>regionvec<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><span class="hljs-punctuation">]</span><br><br>tb4waffles<span class="hljs-operator">$</span><span class="hljs-built_in">names</span> <span class="hljs-operator">&lt;-</span> factor<span class="hljs-punctuation">(</span>regionvec<span class="hljs-punctuation">,</span>levels<span class="hljs-operator">=</span>categ_table<span class="hljs-operator">$</span><span class="hljs-built_in">names</span><span class="hljs-punctuation">)</span><br><br>Color<span class="hljs-operator">&lt;-</span>brewer.pal<span class="hljs-punctuation">(</span>nrow<span class="hljs-punctuation">(</span>categ_table<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&quot;Set2&quot;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs R">ggplot<span class="hljs-punctuation">(</span>tb4waffles<span class="hljs-punctuation">,</span> aes<span class="hljs-punctuation">(</span>x <span class="hljs-operator">=</span> x<span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> y<span class="hljs-punctuation">,</span> fill <span class="hljs-operator">=</span> <span class="hljs-built_in">names</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span><br>  <span class="hljs-comment">#geom_tile(color = &quot;white&quot;) + #</span><br>  geom_point<span class="hljs-punctuation">(</span>color <span class="hljs-operator">=</span> <span class="hljs-string">&quot;black&quot;</span><span class="hljs-punctuation">,</span>shape<span class="hljs-operator">=</span><span class="hljs-number">21</span><span class="hljs-punctuation">,</span>size<span class="hljs-operator">=</span><span class="hljs-number">5</span><span class="hljs-punctuation">)</span> <span class="hljs-operator">+</span> <span class="hljs-comment">#</span><br>  scale_fill_manual<span class="hljs-punctuation">(</span>name <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Category&quot;</span><span class="hljs-punctuation">,</span><br>                    values <span class="hljs-operator">=</span> Color<span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  xlab<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;1 square = 100&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  ylab<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  coord_fixed<span class="hljs-punctuation">(</span>ratio <span class="hljs-operator">=</span> <span class="hljs-number">1</span><span class="hljs-punctuation">)</span><span class="hljs-operator">+</span><br>  theme<span class="hljs-punctuation">(</span><span class="hljs-comment">#panel.border = element_rect(fill=NA,size = 2),</span><br>         panel.background  <span class="hljs-operator">=</span> element_blank<span class="hljs-punctuation">(</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        plot.title <span class="hljs-operator">=</span> element_text<span class="hljs-punctuation">(</span>size <span class="hljs-operator">=</span> rel<span class="hljs-punctuation">(</span><span class="hljs-number">1.2</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span><br>        <span class="hljs-comment">#axis.text = element_blank(),</span><br>        <span class="hljs-comment">#axis.title = element_blank(),</span><br>        <span class="hljs-comment">#axis.ticks = element_blank(),</span><br>        <span class="hljs-comment"># legend.title = element_blank(),</span><br>        legend.position <span class="hljs-operator">=</span> <span class="hljs-string">&quot;right&quot;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h3 id="6-1-4-waffle-包绘制（一个好用的包，专为华夫饼图做准备的）"><a href="#6-1-4-waffle-包绘制（一个好用的包，专为华夫饼图做准备的）" class="headerlink" title="6.1.4 waffle 包绘制（一个好用的包，专为华夫饼图做准备的）"></a>6.1.4 waffle 包绘制（一个好用的包，专为华夫饼图做准备的）</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs R"><span class="hljs-comment">#waffle(parts, rows = 10, keep = TRUE, xlab = NULL, title = NULL, colors = NA, size = 2, flip = FALSE, reverse = FALSE, equal = TRUE, pad = 0, use_glyph = FALSE, glyph_size = 12, legend_pos = &quot;right&quot;)</span><br><span class="hljs-comment">#parts 用于图表的值的命名向量</span><br><span class="hljs-comment">#rows 块的行数</span><br><span class="hljs-comment">#keep 保持因子水平（例如，在华夫饼图中获得一致的图例）</span><br>library<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;waffle&quot;</span><span class="hljs-punctuation">)</span><br>parts <span class="hljs-operator">&lt;-</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span>One<span class="hljs-operator">=</span><span class="hljs-number">80</span><span class="hljs-punctuation">,</span> Two<span class="hljs-operator">=</span><span class="hljs-number">30</span><span class="hljs-punctuation">,</span> Three<span class="hljs-operator">=</span><span class="hljs-number">20</span><span class="hljs-punctuation">,</span> Four<span class="hljs-operator">=</span><span class="hljs-number">10</span><span class="hljs-punctuation">)</span><br>chart <span class="hljs-operator">&lt;-</span> waffle<span class="hljs-punctuation">(</span>parts<span class="hljs-punctuation">,</span> rows<span class="hljs-operator">=</span><span class="hljs-number">8</span><span class="hljs-punctuation">)</span><br>print<span class="hljs-punctuation">(</span>chart<span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h1 id="7-三维散点图绘制"><a href="#7-三维散点图绘制" class="headerlink" title="7 三维散点图绘制"></a>7 三维散点图绘制</h1><h2 id="7-1-简单绘制"><a href="#7-1-简单绘制" class="headerlink" title="7.1 简单绘制"></a>7.1 简单绘制</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs R">library<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;plot3D&quot;</span><span class="hljs-punctuation">)</span><br><span class="hljs-comment">#以Sepal.Length为x轴，Sepal.Width为y轴，Petal.Length为z轴。绘制箱子型box = TRUE;旋转角度为theta = 60, phi = 20;透视转换强度的值为3d=3;按照2D图绘制正常刻度ticktype = &quot;detailed&quot;;散点图的颜色设置bg=&quot;#F57446&quot;</span><br>pmar <span class="hljs-operator">&lt;-</span> par<span class="hljs-punctuation">(</span>mar <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">5.1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4.1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4.1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">6.1</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span> <span class="hljs-comment">#改版画布版式大小</span><br>with<span class="hljs-punctuation">(</span>iris<span class="hljs-punctuation">,</span> scatter3D<span class="hljs-punctuation">(</span>x <span class="hljs-operator">=</span> Sepal.Length<span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> Sepal.Width<span class="hljs-punctuation">,</span> z <span class="hljs-operator">=</span> Petal.Length<span class="hljs-punctuation">,</span><br>  pch <span class="hljs-operator">=</span> <span class="hljs-number">21</span><span class="hljs-punctuation">,</span> cex <span class="hljs-operator">=</span> <span class="hljs-number">1.5</span><span class="hljs-punctuation">,</span>col<span class="hljs-operator">=</span><span class="hljs-string">&quot;black&quot;</span><span class="hljs-punctuation">,</span>bg<span class="hljs-operator">=</span><span class="hljs-string">&quot;#F57446&quot;</span><span class="hljs-punctuation">,</span><br>                   xlab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sepal.Length&quot;</span><span class="hljs-punctuation">,</span><br>                   ylab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sepal.Width&quot;</span><span class="hljs-punctuation">,</span><br>                   zlab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Petal.Length&quot;</span><span class="hljs-punctuation">,</span> <br>                   ticktype <span class="hljs-operator">=</span> <span class="hljs-string">&quot;detailed&quot;</span><span class="hljs-punctuation">,</span>bty <span class="hljs-operator">=</span> <span class="hljs-string">&quot;f&quot;</span><span class="hljs-punctuation">,</span>box <span class="hljs-operator">=</span> <span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span><br>                   theta <span class="hljs-operator">=</span> <span class="hljs-number">60</span><span class="hljs-punctuation">,</span> phi <span class="hljs-operator">=</span> <span class="hljs-number">20</span><span class="hljs-punctuation">,</span> d<span class="hljs-operator">=</span><span class="hljs-number">3</span><span class="hljs-punctuation">,</span><br>                   colkey <span class="hljs-operator">=</span> <span class="hljs-literal">FALSE</span><span class="hljs-punctuation">)</span><br><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h2 id="7-2-加入第四个变量，进行颜色分组"><a href="#7-2-加入第四个变量，进行颜色分组" class="headerlink" title="7.2 加入第四个变量，进行颜色分组"></a>7.2 加入第四个变量，进行颜色分组</h2><h3 id="7-2-1-方法一"><a href="#7-2-1-方法一" class="headerlink" title="7.2.1 方法一"></a>7.2.1 方法一</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs R"><span class="hljs-comment">#可以将变量Petal.Width映射到数据点颜色中。该变量是连续性，如果想将数据按从小到大分成n类，则可以使用dplyr包中的ntile()函数，然后依次设置不同组的颜色bg=colormap[iris$quan]，并根据映射的数值添加图例颜色条（colkey()）。</span><br>library<span class="hljs-punctuation">(</span>tidyverse<span class="hljs-punctuation">)</span><br>iris <span class="hljs-operator">=</span> iris <span class="hljs-operator">%&gt;%</span> mutate<span class="hljs-punctuation">(</span>quan <span class="hljs-operator">=</span> ntile<span class="hljs-punctuation">(</span>Petal.Width<span class="hljs-punctuation">,</span><span class="hljs-number">6</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>colormap <span class="hljs-operator">&lt;-</span> colorRampPalette<span class="hljs-punctuation">(</span>rev<span class="hljs-punctuation">(</span>brewer.pal<span class="hljs-punctuation">(</span><span class="hljs-number">11</span><span class="hljs-punctuation">,</span><span class="hljs-string">&#x27;RdYlGn&#x27;</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">(</span><span class="hljs-number">6</span><span class="hljs-punctuation">)</span><span class="hljs-comment">#legend颜色配置</span><br>pmar <span class="hljs-operator">&lt;-</span> par<span class="hljs-punctuation">(</span>mar <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">5.1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4.1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4.1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">6.1</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br><span class="hljs-comment"># 绘图</span><br>with<span class="hljs-punctuation">(</span>iris<span class="hljs-punctuation">,</span> scatter3D<span class="hljs-punctuation">(</span>x <span class="hljs-operator">=</span> Sepal.Length<span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> Sepal.Width<span class="hljs-punctuation">,</span> z <span class="hljs-operator">=</span> Petal.Length<span class="hljs-punctuation">,</span>pch <span class="hljs-operator">=</span> <span class="hljs-number">21</span><span class="hljs-punctuation">,</span> cex <span class="hljs-operator">=</span> <span class="hljs-number">1.5</span><span class="hljs-punctuation">,</span>col<span class="hljs-operator">=</span><span class="hljs-string">&quot;black&quot;</span><span class="hljs-punctuation">,</span>bg<span class="hljs-operator">=</span>colormap<span class="hljs-punctuation">[</span>iris<span class="hljs-operator">$</span>quan<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br>     xlab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sepal.Length&quot;</span><span class="hljs-punctuation">,</span><br>     ylab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sepal.Width&quot;</span><span class="hljs-punctuation">,</span><br>     zlab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Petal.Length&quot;</span><span class="hljs-punctuation">,</span> <br>     ticktype <span class="hljs-operator">=</span> <span class="hljs-string">&quot;detailed&quot;</span><span class="hljs-punctuation">,</span>bty <span class="hljs-operator">=</span> <span class="hljs-string">&quot;f&quot;</span><span class="hljs-punctuation">,</span>box <span class="hljs-operator">=</span> <span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span><br>     theta <span class="hljs-operator">=</span> <span class="hljs-number">60</span><span class="hljs-punctuation">,</span> phi <span class="hljs-operator">=</span> <span class="hljs-number">20</span><span class="hljs-punctuation">,</span> d<span class="hljs-operator">=</span><span class="hljs-number">3</span><span class="hljs-punctuation">,</span><br>     colkey <span class="hljs-operator">=</span> <span class="hljs-literal">FALSE</span><span class="hljs-punctuation">)</span><br><span class="hljs-punctuation">)</span><br>colkey <span class="hljs-punctuation">(</span>col<span class="hljs-operator">=</span>colormap<span class="hljs-punctuation">,</span>clim<span class="hljs-operator">=</span><span class="hljs-built_in">range</span><span class="hljs-punctuation">(</span>iris<span class="hljs-operator">$</span>quan<span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>clab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Petal.Width&quot;</span><span class="hljs-punctuation">,</span> add<span class="hljs-operator">=</span><span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span> <span class="hljs-built_in">length</span><span class="hljs-operator">=</span><span class="hljs-number">0.4</span><span class="hljs-punctuation">,</span>side <span class="hljs-operator">=</span> <span class="hljs-number">4</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h3 id="7-2-3-方法二"><a href="#7-2-3-方法二" class="headerlink" title="7.2.3 方法二"></a>7.2.3 方法二</h3><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs R"><span class="hljs-comment">#将第四维数据映射到数据点的大小上(cex = rescale(iris$quan, c(.5, 4)))这里我还“得寸进尺”的将颜色也来反应第四维变量，当然也可以用颜色反应第五维变量。</span><br>pmar <span class="hljs-operator">&lt;-</span> par<span class="hljs-punctuation">(</span>mar <span class="hljs-operator">=</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">5.1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4.1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4.1</span><span class="hljs-punctuation">,</span> <span class="hljs-number">6.1</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><br>with<span class="hljs-punctuation">(</span>iris<span class="hljs-punctuation">,</span> scatter3D<span class="hljs-punctuation">(</span>x <span class="hljs-operator">=</span> Sepal.Length<span class="hljs-punctuation">,</span> y <span class="hljs-operator">=</span> Sepal.Width<span class="hljs-punctuation">,</span> z <span class="hljs-operator">=</span> Petal.Length<span class="hljs-punctuation">,</span>pch <span class="hljs-operator">=</span> <span class="hljs-number">21</span><span class="hljs-punctuation">,</span> <br>                     cex <span class="hljs-operator">=</span> rescale<span class="hljs-punctuation">(</span>iris<span class="hljs-operator">$</span>quan<span class="hljs-punctuation">,</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">.5</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>col<span class="hljs-operator">=</span><span class="hljs-string">&quot;black&quot;</span><span class="hljs-punctuation">,</span>bg<span class="hljs-operator">=</span>colormap<span class="hljs-punctuation">[</span>iris<span class="hljs-operator">$</span>quan<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br>                     xlab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sepal.Length&quot;</span><span class="hljs-punctuation">,</span><br>                     ylab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Sepal.Width&quot;</span><span class="hljs-punctuation">,</span><br>                     zlab <span class="hljs-operator">=</span> <span class="hljs-string">&quot;Petal.Length&quot;</span><span class="hljs-punctuation">,</span> <br>                     ticktype <span class="hljs-operator">=</span> <span class="hljs-string">&quot;detailed&quot;</span><span class="hljs-punctuation">,</span>bty <span class="hljs-operator">=</span> <span class="hljs-string">&quot;f&quot;</span><span class="hljs-punctuation">,</span>box <span class="hljs-operator">=</span> <span class="hljs-literal">TRUE</span><span class="hljs-punctuation">,</span><br>                     theta <span class="hljs-operator">=</span> <span class="hljs-number">30</span><span class="hljs-punctuation">,</span> phi <span class="hljs-operator">=</span> <span class="hljs-number">15</span><span class="hljs-punctuation">,</span> d<span class="hljs-operator">=</span><span class="hljs-number">2</span><span class="hljs-punctuation">,</span><br>                     colkey <span class="hljs-operator">=</span> <span class="hljs-literal">FALSE</span><span class="hljs-punctuation">)</span><br><span class="hljs-punctuation">)</span><br>breaks <span class="hljs-operator">=</span><span class="hljs-number">1</span><span class="hljs-operator">:</span><span class="hljs-number">6</span><br>legend<span class="hljs-punctuation">(</span><span class="hljs-string">&quot;right&quot;</span><span class="hljs-punctuation">,</span>title <span class="hljs-operator">=</span>  <span class="hljs-string">&quot;Weight&quot;</span><span class="hljs-punctuation">,</span>legend<span class="hljs-operator">=</span>breaks<span class="hljs-punctuation">,</span>pch<span class="hljs-operator">=</span><span class="hljs-number">21</span><span class="hljs-punctuation">,</span><br>       pt.cex<span class="hljs-operator">=</span>rescale<span class="hljs-punctuation">(</span>breaks<span class="hljs-punctuation">,</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-number">.5</span><span class="hljs-punctuation">,</span> <span class="hljs-number">4</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">)</span><span class="hljs-punctuation">,</span>y.intersp<span class="hljs-operator">=</span><span class="hljs-number">1.6</span><span class="hljs-punctuation">,</span><br>       pt.bg <span class="hljs-operator">=</span> colormap<span class="hljs-punctuation">[</span><span class="hljs-number">1</span><span class="hljs-operator">:</span><span class="hljs-number">6</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>bg<span class="hljs-operator">=</span><span class="hljs-string">&quot;white&quot;</span><span class="hljs-punctuation">,</span>bty<span class="hljs-operator">=</span><span class="hljs-string">&quot;n&quot;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="img"></p>
<h2 id="7-3-用rgl包的plot3d-进行绘制"><a href="#7-3-用rgl包的plot3d-进行绘制" class="headerlink" title="7.3 用rgl包的plot3d()进行绘制"></a>7.3 用rgl包的plot3d()进行绘制</h2><figure class="highlight r"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs R">library<span class="hljs-punctuation">(</span>rgl<span class="hljs-punctuation">)</span><br><span class="hljs-comment">#数据</span><br>mycolors <span class="hljs-operator">&lt;-</span> <span class="hljs-built_in">c</span><span class="hljs-punctuation">(</span><span class="hljs-string">&#x27;royalblue1&#x27;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&#x27;darkcyan&#x27;</span><span class="hljs-punctuation">,</span> <span class="hljs-string">&#x27;oldlace&#x27;</span><span class="hljs-punctuation">)</span><br>iris<span class="hljs-operator">$</span>color <span class="hljs-operator">&lt;-</span> mycolors<span class="hljs-punctuation">[</span> <span class="hljs-built_in">as.numeric</span><span class="hljs-punctuation">(</span>iris<span class="hljs-operator">$</span>Species<span class="hljs-punctuation">)</span> <span class="hljs-punctuation">]</span><br><span class="hljs-comment">#绘制</span><br>plot3d<span class="hljs-punctuation">(</span> <br>  x<span class="hljs-operator">=</span>iris<span class="hljs-operator">$</span>`Sepal.Length`<span class="hljs-punctuation">,</span> y<span class="hljs-operator">=</span>iris<span class="hljs-operator">$</span>`Sepal.Width`<span class="hljs-punctuation">,</span> z<span class="hljs-operator">=</span>iris<span class="hljs-operator">$</span>`Petal.Length`<span class="hljs-punctuation">,</span> <br>  col <span class="hljs-operator">=</span> iris<span class="hljs-operator">$</span>color<span class="hljs-punctuation">,</span> <br>  type <span class="hljs-operator">=</span> <span class="hljs-string">&#x27;s&#x27;</span><span class="hljs-punctuation">,</span> <br>  radius <span class="hljs-operator">=</span> <span class="hljs-number">.1</span><span class="hljs-punctuation">,</span><br>  xlab<span class="hljs-operator">=</span><span class="hljs-string">&quot;Sepal Length&quot;</span><span class="hljs-punctuation">,</span> ylab<span class="hljs-operator">=</span><span class="hljs-string">&quot;Sepal Width&quot;</span><span class="hljs-punctuation">,</span> zlab<span class="hljs-operator">=</span><span class="hljs-string">&quot;Petal Length&quot;</span><span class="hljs-punctuation">)</span><br></code></pre></td></tr></table></figure>

<p><img src="C:\Users\王孙康宏\AppData\Roaming\Typora\typora-user-images\image-20210830203748986.png" srcset="/my-hexo-fluid/img/loading.gif" lazyload alt="image-20210830203748986"></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-hexo-fluid/categories/R/" class="category-chain-item">R</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-hexo-fluid/tags/R/">#R</a>
      
        <a href="/my-hexo-fluid/tags/Data-Analysis/">#Data Analysis</a>
      
        <a href="/my-hexo-fluid/tags/Data-Visualization/">#Data Visualization</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>R language visualization</div>
      <div>https://wskh0929.gitee.io/2023/01/01/R-language-visualization/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>WSKH0929</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年1月1日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-hexo-fluid/2023/01/01/Zookepper%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" title="Zookepper学习笔记">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Zookepper学习笔记</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-hexo-fluid/2023/01/01/Redis%E8%BF%9B%E9%98%B6-%E7%8B%82%E7%A5%9E%E7%AC%94%E8%AE%B0/" title="Redis进阶-狂神笔记">
                        <span class="hidden-mobile">Redis进阶-狂神笔记</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"jkBkMZcW6P8K0bQScoJqvQrX-9Nh9j0Va","appKey":"1k6toLLZa7DFnrENpqSs9Ouw","path":"window.location.pathname","placeholder":"留言仅限讨论，禁止广告等行为","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"https://jkbkmzcw.lc-cn-e1-shared.com","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-hexo-fluid/js/events.js" ></script>
<script  src="/my-hexo-fluid/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-hexo-fluid/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-hexo-fluid/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/love.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxingxing.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/caidai.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-hexo-fluid/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
